//
//  ViewController.m
//  AsteroidGame
//
//  Created by iD Student on 7/11/13.
//  Copyright (c) 2013 iD Student. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    int score;
    UILabel *labelScore;
    int controls;
}
@synthesize screenHeight, screenWidth;
@synthesize labelScore;
@synthesize backToMainMenuButton;
@synthesize highScores;

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"transition"]) {
        
    
        [segue.destinationViewController viewDidLoadWithControls:controlOptions.selectedSegmentIndex];
    } else {
        
    }
}

- (void)viewDidLoadWithControls:(int)controlNumber
{
    highScores = [[NSMutableArray alloc]init];
    highScores = [NSMutableArray arrayWithContentsOfFile:@"highScores"];
    NSLog([NSString stringWithFormat:@"%i", highScores.count]);
    controls = controlNumber;
    [super viewDidLoad];
    density = 3;
    speed = 2;
	// Do any additional setup after loading the view, typically from a nib.
    screenHeight = self.view.frame.size.height;
    screenWidth = self.view.frame.size.width;
    imageRocket = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"rocket.png"]];
    imageRocket.frame = CGRectMake(screenWidth/2 - 30, screenHeight*4/5, 27, 39.5);
    [self.view addSubview:imageRocket];
    if(controls == 0)
    {
        [[UIAccelerometer sharedAccelerometer]setDelegate:self];
        [[UIAccelerometer sharedAccelerometer]setUpdateInterval:.01];
    }
    spawnAsteroids = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(spawnAsteroids) userInfo:nil repeats:YES];
    updateAsteroids = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateAsteroids) userInfo:nil repeats:YES];
    asteroidArray = [[NSMutableArray alloc]init];
    score = 0;
    backToMainMenuButton.hidden = true;
}


- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    float x = acceleration.x;
    float y = (-1.0) * acceleration.y;
    imageRocket.center = CGPointMake(screenWidth/2.0 + screenWidth/2.0*x, screenHeight/2.0 + screenHeight/2.0*y);
       
    
    //NSLog([NSString stringWithFormat:@"%f %f %i", x, y, difference]);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)spawnAsteroids {
    Asteroid *asteroid = [[Asteroid alloc]initWithRandomValueX:screenWidth withY:screenHeight];
    [asteroidArray addObject:asteroid];
    [self.view addSubview:asteroid.imageView];
    //NSLog([NSString stringWithFormat:@"%f", asteroid.imageView.center.x]);
    score++;
    labelScore.text = [NSString stringWithFormat:@"Score: %i", score];
}

- (void)updateAsteroids {
    for (int i = 0; i < asteroidArray.count; i++) {
        Asteroid *a = [asteroidArray objectAtIndex:i];
        
        [[asteroidArray objectAtIndex:i] updateWithVelocityX:0 withVelocityY:speed];
        /*CGPoint top = CGPointMake(a.imageView.frame.origin.x, a.imageView.frame.origin.y - a.imageView.frame.size.height/2);
        CGPoint left = CGPointMake(a.imageView.frame.origin.x - a.imageView.frame.size.width/2, a.imageView.frame.origin.y);
        CGPoint bottom = CGPointMake(a.imageView.frame.origin.x, a.imageView.frame.origin.y + a.imageView.frame.size.height/2);
        CGPoint right = CGPointMake(a.imageView.frame.origin.x + a.imageView.frame.size.width/2, a.imageView.frame.origin.y);*/
        if(CGRectIntersectsRect(imageRocket.frame, a.imageView.frame))
        {
            [updateAsteroids invalidate];
            [spawnAsteroids invalidate];
            [[UIAccelerometer sharedAccelerometer]setUpdateInterval:1000000000000];
            backToMainMenuButton.hidden = false;
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            [highScores addObject:[NSString stringWithString:labelScore.text]];
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSLog(labelScore.text);
            NSString *path = [documentsDirectory stringByAppendingPathComponent:@"/highScores.plist"];
            NSLog(path);
            [highScores writeToFile:path atomically:YES];
            NSLog([NSString stringWithFormat:@"%@", labelScore.text]);
            NSLog([NSString stringWithFormat:@"%i", highScores.count]);
        }
        else {
            if (a.imageView.frame.origin.y > (screenHeight + a.imageView.frame.size.height)) {
            [asteroidArray removeObject:a];
            [a.imageView removeFromSuperview];
            } else {
        }
    }
    }
}

- (void)spawnParticle {
    
}

- (void)updateParticle
{
    
}


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (controls == 1) {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.view];
        imageRocket.center = point;
    }
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (controls == 1) {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.view];
        imageRocket.center = point;
    }
}

@end
